package mammuthus.web.action

import java.util
import javax.servlet.http.{HttpServletResponse, HttpServletRequest}
import com.webdm.common.cookie.CookiesInfo
import mammuthus.web.util.AuthUtil
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.{RequestParam, RequestMethod, RequestMapping}

import scala.collection.JavaConversions._

/**
 * Created by fengbingjian on 15/5/18 13:03.
 */
@Controller
class LoginAction {

  @RequestMapping(value = Array("l.html"), method = Array(RequestMethod.GET))
  def loginGet():String = {

    "login"
  }

  @RequestMapping(value = Array("l.html"), method = Array(RequestMethod.POST))
  def login(req: HttpServletRequest,httpServletResponse: HttpServletResponse,
            @RequestParam(value = "name", defaultValue = "") name: String,
            @RequestParam(value = "pwd", defaultValue = "") pwd: String):String = {


    var error = ""
    if(name == ""){
      error = "用户名不能为空!"
    }else if(pwd == "")
      error = "密码不能为空!"
    else if("mammuthus" == name &&  "mammuthus1289" == pwd){

      //mammuthus/mammuthus1289
      val authKey = AuthUtil.createKey(name)
      CookiesInfo.addNewCookie(AuthUtil.cookieKey, authKey, -1, httpServletResponse)
      //登录成功
      return "redirect:app/appInstalled.html"
    }else{
      error = "用户名或密码错误!"
    }


    req.setAttribute("name", name)
    req.setAttribute("pwd", pwd)
    req.setAttribute("msg", error)
    "login"
  }



}
